package com.zj.service;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.zj.web.bean.MyUserDTO;
import com.zj.web.bean.OrderMessageDTO;
import com.zj.web.bean.OrdersDTO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
@Log
public class JmsMessageConsumer {
    @Autowired
    private VelocityTemplateService velocityTemplateService;

    @Autowired
    private MailService mailService;
    @Value("${spring.mail.from}")
    private String from;//邮件发送方，这里指定为中转邮箱
    /**
     * 监听队列名为userLogin的消息，然后处理
     * @param message
     */
    @JmsListener(destination = "userLoginQueue")
    public void receiveMessage(String message) {
        log.info("收到消息: " + message);
        Gson gson = new Gson();
        MyUserDTO user = gson.fromJson(message, MyUserDTO.class);  //将消息转换为用户对象,跟切面保持一种
        log.info("用户登录成功, 用户名: " + user.toString());

        String emailInfo=velocityTemplateService.userLoginSuccessInfo(user);
        log.info("待发送的邮件内容: "+emailInfo);
        mailService.send(user.getEmail(),from,"用户登录成功",emailInfo);
    }
    //监听验证码队列
    @JmsListener(destination = "verifyCodeQueue")
    public void receiveVerifyCodeMessage(String message) {
        log.info("收到验证码消息: " + message);
        Gson gson = new Gson();
        MyUserDTO user = gson.fromJson(message, MyUserDTO.class);  //将消息转换为用户对象,跟切面保持一种
        log.info("收到的验证码用户信息: " + user.toString());
        String emailInfo=velocityTemplateService.registerVerifyCodeInfo(user);
        log.info("待发送的验证码邮件内容: "+emailInfo);
        mailService.send(user.getEmail(),from,"用户注册验证码",emailInfo);
    }
    //监听订单队列
    @JmsListener(destination = "orderSuccessQueue")
    public void receiveOrderMessage(String message) {
        log.info("收到订单成功的消息: " + message);
        try {
            Gson gson = new Gson();
            OrderMessageDTO order = gson.fromJson(message, OrderMessageDTO.class);  //将消息转换为订单对象,跟切面保持一种
            log.info("收到的订单信息: " + order.toString());
            String emailInfo=velocityTemplateService.orderSuccessInfo(order);
            log.info("待发送的订单成功邮件内容: "+emailInfo);
            mailService.send(order.getEmail(),from,"订单成功",emailInfo);
        } catch (Exception e) {
            log.warning("发送订单成功邮件失败: " + e.getMessage());
        }
    }
    //监听订单状态队列
    @JmsListener(destination = "orderStatusQueue")
    public void receiveOrderStatusMessage(String message) {
        log.info("收到订单状态更新的消息: " + message);
        try {
            Gson gson = new Gson();
            OrderMessageDTO order = gson.fromJson(message, OrderMessageDTO.class);  //将消息转换为订单对象,跟切面保持一种
            log.info("收到的订单状态更新信息: " + order.toString());
            String emailInfo=velocityTemplateService.orderStatusSuccessInfo(order);
            log.info("待发送的订单状态更新邮件内容: "+emailInfo);
            mailService.send(order.getEmail(),from,"订单状态更新",emailInfo);
        } catch (Exception e) {
            log.warning("发送订单状态更新邮件失败: " + e.getMessage());
        }
    }

}
